package cn.lena.idle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.Toast;

import java.util.List;

import cn.lena.idle.dao.CatagoryDbHelper;
import cn.lena.idle.dao.GoodsDbHelper;
import cn.lena.idle.pojo.Catagory;
import cn.lena.idle.pojo.DTO.LoginUser;
import cn.lena.idle.pojo.DTO.Version;
import cn.lena.idle.pojo.Goods;
import cn.lena.idle.util.DateUtils;
import cn.lena.idle.util.PhotoUtils;

/**
 * @anthor lena
 * @date 2021/6/11
 */
public class GoodsAddActivity extends AppCompatActivity {

    private ImageButton photo;
    private ImageView back;
    private Button add;
    private EditText title;
    private EditText describe;
    private EditText catagory;
    private EditText price;
    private EditText address;

    private List<Catagory> catagories;
    private PopupMenu pm;
    private Integer cid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_add);

        photo=(ImageButton)findViewById(R.id.add_photo);
        add=(Button) findViewById(R.id.add_button);
        title=(EditText)findViewById(R.id.add_title);
        describe=(EditText)findViewById(R.id.add_describe);
        catagory=(EditText)findViewById(R.id.add_catagory);
        price=(EditText)findViewById(R.id.add_price);
        address=(EditText)findViewById(R.id.add_address);
        back=(ImageView)findViewById(R.id.returnbefore3);

        // 返回上一页
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(GoodsAddActivity.this,MyGoodsActivity.class));
            }
        });

        // 发布商品按钮
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addGoods();
            }
        });

        // 单击图片按钮时，能够在相册中选择图片
        photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent,1);
            }
        });

        // 初始化菜单项 t
        initMenu();

        // 选择分类后添加下拉菜单项 t
        catagory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 显示菜单项
                pm.show();
            }
        });

    }

    /**
     * 选择图片后触发事件 t
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == 1) {
            //从相册返回的数据
            if (data != null) {
                //得到图片的全路径
                Uri uri = data.getData();
                photo.setImageURI(uri);
            }
        }
    }

    /**
     * 添加商品
     */
    private void addGoods(){

        // 初始化商品的值
        Goods goods=new Goods();
        goods.setCatagoryId(cid);
        goods.setGoodsName(title.getText().toString().trim());
        goods.setDescribe(describe.getText().toString().trim());
        goods.setPicture(PhotoUtils.ImageButtonToByte(photo));
        goods.setPrice(price.getText().toString().trim());
        goods.setPhone(LoginUser.CURRENT_USER.getPhone());
        goods.setPublishTime(DateUtils.getNowDate2());
        goods.setStatus(0);
        goods.setUsername(LoginUser.CURRENT_USER.getUsername());
        goods.setAddress(address.getText().toString().trim());
        goods.setUserId(LoginUser.CURRENT_USER.getId());

        // 数据库操作
        GoodsDbHelper goods_db=new GoodsDbHelper(getApplicationContext(),GoodsDbHelper.DB_NAME,null,Version.GOODS);
        int result = goods_db.addGoods(goods);

        // 添加结果
        if(result > 0 ) {
            Toast.makeText(this, "发布商品“"+goods.getGoodsName()+"”成功！", Toast.LENGTH_LONG).show();
            // 跳转到我的发布
            Intent intent=new Intent(GoodsAddActivity.this,MyGoodsActivity.class);
            startActivity(intent);
        }else{
            Toast.makeText(this, "发布失败！", Toast.LENGTH_LONG).show();
            return;
        }

    }

    /**
     * 初始化菜单项 t
     */
    private void initMenu(){

        pm=new PopupMenu(GoodsAddActivity.this,catagory);
        Menu menu=pm.getMenu();
        pm.getMenuInflater().inflate(R.menu.catagory,menu);

        // 查询所有分类
        findAll();

        // 将分类全部添加到菜单项中
        for(Catagory c:catagories){
            // 1-组别、2-菜单项id、3-菜单项顺序、4-菜单项内容
            menu.add(0,c.getId(),0,c.getName());
        }

        // 添加单击数据项事件
        pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                catagory.setText(item.getTitle());
                cid=item.getItemId();
                return true;
            }
        });

    }


    /**
     * 查询所有分类 t
     */
    private void findAll(){
        CatagoryDbHelper catagory_db=new CatagoryDbHelper(getApplicationContext(),CatagoryDbHelper.DB_NAME,null, Version.CATAGORY);
        catagories = catagory_db.getAll();
    }


}
